package com.beiji.lib.pen.ui.renderer

import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import com.beiji.lib.pen.model.DotUnit
import com.beiji.lib.pen.model.PenLine
import com.beiji.lib.pen.model.PenStroke
import com.tqltech.tqlpencomm.bean.Dot

/**
 * 线段渲染器
 * Created by X on 2018/6/21.
 */
class LineRenderer : Renderer() {

    var lastX: Int = -1
    var lastY: Int = -1

    override fun reset() {
        lastX = -1
        lastY = -1
    }

    override fun draw(canvas: Canvas, stroke: PenStroke, dotMatrix: Matrix, paint: Paint?) {
    }

    override fun draw(canvas: Canvas, dot: DotUnit, paint: Paint?) {
        val penLine = PenLine()
        penLine.pageId = dot.pageId
        penLine.force = dot.force
        penLine.x1 = dot.x.toInt()
        penLine.y1 = dot.y.toInt()

        if (dot.type == Dot.DotType.PEN_DOWN || lastX == -1) {
            penLine.x2 = penLine.x1
        } else {
            penLine.x2 = lastX
        }
        if (dot.type == Dot.DotType.PEN_DOWN || lastY == -1) {
            penLine.y2 = penLine.y1
        } else {
            penLine.y2 = lastY
        }

        lastX = penLine.x1
        lastY = penLine.y1
        canvas.drawLine(penLine.x1.toFloat(), penLine.y1.toFloat(),
                penLine.x2.toFloat(), penLine.y2.toFloat(), paint!!)
    }
}